﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;

namespace RoAssistant.Client
{
    /// <summary>
    /// client控件主函数
    /// </summary>
    class FindClientMain
    {
        /// <summary>
        /// 查询client控件
        /// </summary>
        /// <param name="windowname">程序名称</param>
        public static void FindClientMainFunc(string windowname)
        {
            //初始私有方法
            PirvateFunc prPirvateFunc = new PirvateFunc();
            //获取桌面程序
            PubArgs.DesktopElement = AutomationElement.RootElement;
            //创建一个List列表存放桌面程序
            PubArgs.ElementInfos = new List<ClientElement>();
            //初始化
            ChangeListToTci changeListToTci = new ChangeListToTci();

            //获取程序并设定为焦点程序
            prPirvateFunc.SetTestWindowToFocused(PubArgs.DesktopElement, windowname);
            //写入List
            ClientElement winInfo = prPirvateFunc.GetSigleElementInfo(PubArgs.FocusedElement, "Desktop");
            PubArgs.ElementInfos.Add(winInfo);
            //分拆剩余节点元素
            prPirvateFunc.AnalyzeElementCount(PubArgs.FocusedElement, winInfo.ControlId);
            //写入List并导出
            changeListToTci.ChangeListToTciFile(PubArgs.ElementInfos);
        }
    }

    /// <summary>
    /// 私有方法
    /// </summary>
    class PirvateFunc
    {
        /// <summary>
        /// 控件信息写成一个类
        /// </summary>
        /// <param name="sigleElement">单个自动化元素</param>
        /// <param name="parentvalue">parentvalue值</param>
        /// <returns>返回单个元素信息</returns>
        public ClientElement GetSigleElementInfo(AutomationElement sigleElement, string parentvalue)
        {
            ClientElement elementInfo = new ClientElement
            {
                ControlType = sigleElement.Current.ControlType,
                ReferenceContainer = parentvalue,
                ElementAutomationId = sigleElement.Current.AutomationId,
                ElementName = sigleElement.Current.Name,
                ElementClassName = sigleElement.Current.ClassName
            };
            elementInfo.ControlId = elementInfo.ElementAutomationId + elementInfo.ElementName;
            return elementInfo;
        }


        /// <summary>
        /// 寻找待测试的程序
        /// </summary>
        /// <param name="rootElement">根元素</param>
        /// <param name="windowname">桌面名称</param>
        public void SetTestWindowToFocused(AutomationElement rootElement, string windowname)
        {
            try
            {
                AutomationElementCollection findCollection = rootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
                foreach (AutomationElement sigElement in findCollection)
                {
                    AutomationElement.AutomationElementInformation current = sigElement.Current;
                    bool flag = current.Name.Equals(windowname);
                    if (flag)
                    {
                        PubArgs.FocusedElement = sigElement;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        /// <summary>
        /// 解析非root节点
        /// </summary>
        /// <param name="classAElemet">主程序元素</param>
        /// <param name="classAElemetId">主程序ID</param>
        public void AnalyzeElementCount(AutomationElement classAElemet, string classAElemetId)
        {
            try
            {
                for (AutomationElement elementNode = TreeWalker.RawViewWalker.GetFirstChild(classAElemet); elementNode != null; elementNode = TreeWalker.RawViewWalker.GetNextSibling(elementNode))
                {
                    ClientElement elementInfos = GetSigleElementInfo(elementNode, classAElemetId);
                    PubArgs.ElementInfos.Add(elementInfos);
                    AnalyzeElementCount(elementNode, elementInfos.ControlId);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
